package com.bank.ui;

import com.bank.service.AuthService;
import com.bank.ui.admin.AdminDashboard;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class AdminLoginUI extends JFrame {
    private JTextField adminIDField;
    private JPasswordField passwordField;

    public AdminLoginUI() {
        setTitle("管理员登录");
        setSize(400, 250); // 增加高度以容纳状态信息
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加状态面板
        JPanel statusPanel = new JPanel();
        JButton testDBButton = new JButton("测试数据库连接");
        testDBButton.addActionListener(e -> testDatabaseConnection());
        statusPanel.add(testDBButton);
        mainPanel.add(statusPanel, BorderLayout.NORTH);

        // 创建表单面板
        JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 15)); // 增加一行用于状态显示

        // 管理员ID输入
        formPanel.add(new JLabel("管理员ID:"));
        adminIDField = new JTextField();
        formPanel.add(adminIDField);

        // 密码输入
        formPanel.add(new JLabel("密码:"));
        passwordField = new JPasswordField();
        formPanel.add(passwordField);

        // 状态标签
        formPanel.add(new JLabel("状态:"));
        JLabel statusLabel = new JLabel("未测试");
        statusLabel.setForeground(Color.RED);
        formPanel.add(statusLabel);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));

        // 登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(this::login);
        buttonPanel.add(loginButton);

        // 返回按钮
        JButton backButton = new JButton("返回");
        backButton.addActionListener(e -> {
            new LoginUI().setVisible(true);
            dispose();
        });
        buttonPanel.add(backButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        setContentPane(mainPanel);

        // 自动测试数据库连接
        testDatabaseConnection();
    }

    private void testDatabaseConnection() {
        try {
            AuthService.testDBConnection();
            showStatus("✅ 数据库连接正常", Color.GREEN);
        } catch (Exception e) {
            showStatus("❌ 数据库连接失败: " + e.getMessage(), Color.RED);
            e.printStackTrace();
        }
    }

    private void showStatus(String message, Color color) {
        // 更新状态标签
        Component[] components = ((JPanel) getContentPane().getComponent(1)).getComponents();
        for (Component comp : components) {
            if (comp instanceof JLabel && "状态:".equals(((JLabel) comp).getText())) {
                JLabel statusLabel = (JLabel) ((JPanel) comp.getParent()).getComponent(5);
                statusLabel.setText(message);
                statusLabel.setForeground(color);
                break;
            }
        }
    }

    private void login(ActionEvent e) {
        String adminID = adminIDField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();

        if (adminID.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入管理员ID和密码！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            // 调用AuthService进行管理员登录验证
            boolean success = AuthService.adminLogin(adminID, password);

            if (success) {
                JOptionPane.showMessageDialog(this, "登录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                openAdminDashboard();
            } else {
                JOptionPane.showMessageDialog(this, "登录失败：管理员ID或密码错误！", "登录失败", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,
                    "登录过程中发生错误:\n" + ex.getMessage(),
                    "系统错误",
                    JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }

    private void openAdminDashboard() {
        AdminDashboard adminDashboard = new AdminDashboard();
        JFrame adminFrame = new JFrame("管理员仪表盘");
        adminFrame.setContentPane(adminDashboard);
        adminFrame.setSize(800, 600); // 设置窗口大小
        adminFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 设置关闭操作
        adminFrame.setVisible(true); // 显示窗口
        dispose(); // 关闭登录窗口
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            AdminLoginUI adminLoginUI = new AdminLoginUI();
            adminLoginUI.setVisible(true);
        });
    }
}